'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'ajaxTab'], function ($) {
    return {
        init: function () {

            this.initPage();
            this.beforeSubmit();
            
        },

        initPage: function () {
        	
        	
            $(document).ready(function(){
                
                $(".ajax-tab").ajaxtab();
                $("#user-resource").ajaxPagination();
                $("#service-supplier").ajaxPagination();
                
            });

            $(document).on("shown.bs.modal", "#edit", function (e) {

                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                
                $form.shiftform({
                    beforeSubmit: function (el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })

            });
        },
        
        beforeSubmit : function() {
            
            $("#transport-supplier").submit(function(e){
                  
                  //validate checkbox, at lest select one
                  var isChooseOne = false;
                  var checkbox = $('input[name = organizations]');
                  for(var i = 0; i < checkbox.length; i++){
                      if(checkbox[i].checked)
                          isChooseOne = true;
                  }
                  if(!isChooseOne){
                      var html = '<div class="tooltip fade bottom in validate-checkbox" role="tooltip" id="tooltip620143" style="display: block; left: 50%; top: 70%; margin-left: -75px;">' +
                                       '<div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div>' +
                                       '<div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);">'+
                                           '<i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;权限不能为空！'+
                                           '<div id="tlt_1499765080332" <="" div=""></div>'+
                                       '</div>'+
                                   '</div>';
                      $("#transport-supplier").append(html);
                      setTimeout("$('.validate-checkbox').remove();",1000)
                      return false;
                  }
                  
            });
            
         },
        
    }
})

